<?php

require_once('../func.php');
require_once('../core.php');

if(file_exists(APP_ROOT."caches/install.lock")) {
  exit('已经安装过FSDKP,请删除caches目录下的install.lock文件再执行此操作');
}

$s_sqlite = function_exists('sqlite_open');
$s_mysql  = function_exists('mysql_connect');

if(!$s_sqlite && !$s_mysql) exit('主机不支持FSDKP安装环境,请配置相关php.ini');

function __autoload($class_name) {
  $classFile = APP_ROOT.'includes/class/'.$class_name.'.php';
  if(file_exists($classFile)) {
    require_once($classFile);      
  }
}

function install_sqlite() {
  $dbpre   = 'fsdkp_';
  $username = trim($_POST['username']);
  $password = trim($_POST['password']);
  $AUTH = time() . random();
  $dbfile = random(10) . '.db';
  $sql_file = realpath('sqlite.sql');
  $db = new Sqlite(array(
    'dbhost'  => APP_ROOT . "caches/" . $dbfile,
    'dbpre'  => $dbpre
  ));
 
  $dbpre   = preg_replace("/([^A-Za-z0-9\-_])+/", '', $dbpre);
  $sqls = @fread(@fopen($sql_file, 'r'), @filesize($sql_file));
  $sqls = preg_replace('/fsdkp\_(\S+?)([\s\.,]|$)/', $dbpre . '\\1\\2', $sqls);
  $sqls_array = explode(";",$sqls);
  foreach($sqls_array as $_=>$val) {
    $val = trim($val);
    if(!empty($val))  $db->query($val);
  }
  $password = md5($password);
  $username = encrypt($username,$AUTH.'');
  $db->query("INSERT INTO @_@users (user_name, user_pwd, user_rank) VALUES ('$username', '$password', '8888')");
  $db->query("INSERT INTO @_@configs (name, val) VALUES ('hash', '$AUTH')");
  
  $configs  = '<?php'."\n\n";
  $configs .= 'define("AUTH", "'. $AUTH .'");' . "\n\n";
  $configs .= '$sqlite = true;' . "\n\n";
  $configs .= '$dbhost = "'.$dbfile.'";' . "\n\n";
  $configs .= '?>';
  $config_file = APP_ROOT."caches/config.php";
  $fp = fopen($config_file, 'w');
	fwrite($fp, $configs);
	fclose($fp);
  $install_file = APP_ROOT.'caches/install.lock';
  $fp = fopen($install_file, 'w');
	fwrite($fp, " ");
	fclose($fp);
  redirect("../admin/");
}

function install_mysql() {
  $dbhost   = trim($_POST['dbhost']);
  $dbuser   = trim($_POST['dbuser']);
  $dbpw     = trim($_POST['dbpass']);
  $dbname   = trim($_POST['dbname']);
  $dbpre   = trim($_POST['dbpre']);
  $username = trim($_POST['username']);
  $password = trim($_POST['password']);
  
  $AUTH = time() . random();
  $sql_file = realpath('mysql.sql');
  $db = new Mysql(array(
    'dbhost'   => $dbhost,
    'dbuser'   => $dbuser,
    'dbpw'     => $dbpw,
    'dbname'   => $dbname,
    'dbpre'    => $dbpre
  ));
  
  $dbpre   = preg_replace("/([^A-Za-z0-9\-_])+/", '', $dbpre);
  $sqls = @fread(@fopen($sql_file, 'r'), @filesize($sql_file));
  $sqls = preg_replace('/fsdkp\_(\S+?)([\s\.,]|$)/', $dbpre . '\\1\\2', $sqls);
  $sqls_array = explode(";",$sqls);
  foreach($sqls_array as $_=>$val) {
    $val = trim($val);
    if(!empty($val))  $db->query($val);
  }
  $password = md5($password);
  $username = encrypt($username,$AUTH.'');
  $db->query("INSERT INTO @_@users (user_name, user_pwd, user_rank) VALUES ('$username', '$password', '8888')");
  $db->query("INSERT INTO @_@configs (name, val) VALUES ('hash', '$AUTH')");
  
  $configs  = '<?php'."\n\n";
  $configs .= 'define("AUTH", "'. $AUTH .'");' . "\n\n";
  $configs .= '$mysql = true;' . "\n\n";
  $configs .= '$dbhost  = "'.$dbhost.'";'."\n\n";
  $configs .= '$dbuser  = "'.$dbuser.'";'."\n\n";
  $configs .= '$dbpw    = "'.$dbpw.'";'."\n\n";
  $configs .= '$dbname  = "'.$dbname.'";'."\n\n";
  $configs .= '$dbpre   = "'.$dbpre.'";'."\n\n";
  $configs .= '?>';
  $config_file = APP_ROOT."caches/config.php";
  $fp = fopen($config_file, 'w');
	fwrite($fp, $configs);
	fclose($fp);
  $install_file = APP_ROOT.'caches/install.lock';
  $fp = fopen($install_file, 'w');
	fwrite($fp, " ");
	fclose($fp);
  redirect("../admin/");
}

if ($_GET['step']=='sqlite') { install_sqlite(); }
elseif($_GET['step']=='mysql') { install_mysql(); }

?>

<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>
  <head>
    <title> FSDKP install </title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" >
    <style>
      <!--
      *{margin:0; padding:0;}
      img {border:0;}
      a:link {text-decoration:none;color:#09F;outline:none;blr:expression(this.onFocus=this.blur());}
      body {color:#666;font-family:"宋体", Arial;font-size:12px;background:#fff url(../admin/themes/bg_login.jpg) no-repeat scroll center 0;}
      li {list-style:none;line-height:28px;height:28px;}
      .blank {height:192px;clear:both;}
      .left {width:46%;float:left;text-align:right;}
      .right {width:48%;float:right;}
      .w160 {width:165px;}
      .w100 {width:100px;}
      .btn {background:url(../admin/themes/bg_button.gif) no-repeat;cursor:pointer;width:80px;height:20px;border:none;line-height:20px;}
      .red {color:#ee0000;}
      #sqlite,#mysql {display:none;}
      -->
    </style>
  </head>
  
  <body>
    <div class="blank"></div>
    <div class="left">
      
    </div>
    <div class="right">
      FSDKP安装:
      <select onchange="ins(this.options[selectedIndex].value)">
        <option value="0">请选择数据库类型</option>
        <?php if($s_sqlite){ ?><option value="sqlite">SQLITE</option><?php } ?>
        <?php if($s_mysql){ ?><option value="mysql">MYSQL</option><?php } ?>
      </select>
      
      <div id="sqlite">
      <form name="form" method="post" action="?step=sqlite">
      <ul>
        <li><h3> 安装: 数据库为SQLITE </h3></li>
        <li> 管理员： <input name="username" class="w160" maxlength="20" type="text" value="admin" /></li>
        <li> 密　码： <input name="password" class="w160" maxlength="32" type="password" /> <img src="themes/lock.gif"/></li>
        <li style="padding-left:48px;">
          <input type="submit" class="btn" value=" 开始安装 " />
        </li>        
      </ul>
      </form>
      </div>
      
      <div id="mysql">
      <form method="post" action="?step=mysql">
      <ul>
        <li><h3> 安装: 数据库为MYSQL </h3></li>
        <li> 主　机： <input name="dbhost" class="w160" maxlength="20" type="text" value="localhost" /></li>
        <li> 用　户： <input name="dbuser" class="w160" maxlength="20" type="text" /></li>
        <li> 密　码： <input name="dbpass" class="w160" maxlength="20" type="password" /></li>
        <li> 数据库： <input name="dbname" class="w160" maxlength="20" type="text" /></li>
        <li> 前　缀： <input name="dbpre" class="w160" maxlength="20" type="text" value="fsdkp_" /></li>
        <li> 管理员： <input name="username" class="w160" maxlength="20" type="text" value="admin" /></li>
        <li> 密　码： <input name="password" class="w160" maxlength="32" type="password" /> <img src="themes/lock.gif"/></li>
        <li style="padding-left:48px;">
          <input type="submit" class="btn" value=" 开始安装 " />
        </li>        
      </ul>
      </form>
      </div>
    
    </div>
    
    <script>
      function ins(id) {
        if(id!='0') {
          document.getElementById('sqlite').style.display = "none";
          document.getElementById('mysql').style.display = "none";
          document.getElementById(id).style.display = "block";
        }
      }
    </script>
    
  </body>
  
</html>
